package whu.o2o;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import whu.o2o.util.ConstantData;
import whu.o2o.vo.PersonVO;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;

public class MyApplication extends Application {
	private ExecutorService mThreadPool;
	private SharedPreferences mSharedPreferences;
	private String mCity;

	/** 用户类型 0 游客 1 达人 2 导游 */
	private int mUserType;
	private int mUserId;
	private PersonVO mUserVO;
	@Override
	public void onCreate() {
		super.onCreate();
		initUserVO();
		//startService(new Intent(this, MyService.class));
	}
	
	private void initUserVO() {
		getUserVO();
		getUserId();
		getUserType();
		getCity();
		
		String name = getSharedPreferences().getString(ConstantData.SharedPrefs.USER_NAME, "");
		if (!TextUtils.isEmpty(name)) {
			mUserVO.setName(name);
		}

		String headUrl = getSharedPreferences().getString(ConstantData.SharedPrefs.USER_HEAD_URL, "");
		if (!TextUtils.isEmpty(headUrl)) {
			mUserVO.setHeadUrl(headUrl);
		}
		
		String cardImageUrl = getSharedPreferences().getString(ConstantData.SharedPrefs.USER_CARD_IMAGE_URL, "");
		if (!TextUtils.isEmpty(cardImageUrl)) {
			mUserVO.setCardImageUrl(cardImageUrl);
		}
	}
	
	public ExecutorService getThreadPool() {
		if (mThreadPool == null) {
			mThreadPool = Executors.newFixedThreadPool(ConstantData.Data.THREAD_POOL_MAX_SIZE);
		}
		return mThreadPool;
	}

	public SharedPreferences getSharedPreferences() {
		if (mSharedPreferences == null) {
			mSharedPreferences = getSharedPreferences(ConstantData.SharedPrefs.NAME, Context.MODE_PRIVATE);
		}
		return mSharedPreferences;
	}

	public void setCity(String city) {
		getSharedPreferences().edit().putString(ConstantData.SharedPrefs.CITY, city).commit();
		mCity = city;
	}

	public String getCity() {
		if (TextUtils.isEmpty(mCity)) {
			mCity = getSharedPreferences().getString(ConstantData.SharedPrefs.CITY, "");
		}
		return mCity;
	}

	public void setUserType(int type) {
		getSharedPreferences().edit().putInt(ConstantData.SharedPrefs.USER_TYPE, type).commit();
		mUserType = type;
	}

	public int getUserType() {
		if (mUserType == 0) {
			mUserType = getSharedPreferences().getInt(ConstantData.SharedPrefs.USER_TYPE, 0);
		}
		return mUserType;
	}

	public void setUserId(int id) {
		getSharedPreferences().edit().putInt(ConstantData.SharedPrefs.USER_ID, id).commit();
		mUserId = id;
	}
	

    
	public int getUserId() {
		if (mUserId == 0) {
			mUserId = getSharedPreferences().getInt(ConstantData.SharedPrefs.USER_ID, 0);
		}
		return mUserId;
	}

	public PersonVO getUserVO() {
		if (mUserVO == null) {
			mUserVO = new PersonVO();
		}
		return mUserVO;
	}

	public void saveUserVO(PersonVO user) {
		mUserVO = user;
		setUserType(mUserVO.getType());
		setUserId(mUserVO.getId());

		if (!TextUtils.isEmpty(mUserVO.getName())) {
			getSharedPreferences().edit().putString(ConstantData.SharedPrefs.USER_NAME, mUserVO.getName()).commit();
		}

		if (!TextUtils.isEmpty(mUserVO.getHeadUrl())) {
			getSharedPreferences().edit().putString(ConstantData.SharedPrefs.USER_HEAD_URL, mUserVO.getHeadUrl())
					.commit();
		}

		if (!TextUtils.isEmpty(mUserVO.getCardImageUrl())) {
			getSharedPreferences().edit()
					.putString(ConstantData.SharedPrefs.USER_CARD_IMAGE_URL, mUserVO.getCardImageUrl()).commit();
		}

	}

}
